
package com.wbtech.ums.common;

import android.content.Context;
import android.os.Build;

import com.wbtech.ums.objects.PostObjActivity;
import com.wbtech.ums.objects.PostObjError;
import com.wbtech.ums.objects.PostObjEvent;
import com.wbtech.ums.objects.PostObjTag;

import org.json.JSONException;
import org.json.JSONObject;

public class AssembJSONObj {

    public static JSONObject getErrorInfoJSONObj(String error, Context context) {
        String stacktrace = error;
        String activities = CommonUtil.getActivityName(context);
        String time = CommonUtil.getTime();
        String appkey = CommonUtil.getAppKey(context);
        String os_version = CommonUtil.getOsVersion(context);
        String deviceID = CommonUtil.getDeviceID(context);
        JSONObject errorInfo = new JSONObject();
        try {
            Build bd = new Build();
            errorInfo.put("stacktrace", stacktrace);
            errorInfo.put("time", time);
            errorInfo.put("activity", activities);
            errorInfo.put("appkey", appkey);
            errorInfo.put("os_version", os_version);
            errorInfo.put("deviceid", bd.MANUFACTURER + bd.PRODUCT);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return errorInfo;
    }
    
    public static PostObjError getErrorInfoObject(String error, Context context) {
    	PostObjError objError = new PostObjError();
    	
        String stacktrace = error;
        String activities = CommonUtil.getActivityName(context);
        String time = CommonUtil.getTime();
        String appkey = CommonUtil.getAppKey(context);
        String os_version = CommonUtil.getOsVersion(context);
        
        Build bd = new Build();
        objError.setStacktrace(stacktrace);
        objError.setTime(time);
        objError.setActivity(activities);
        objError.setAppkey(appkey);
        objError.setOs_version(os_version);
        objError.setDeviceid(bd.MANUFACTURER + bd.PRODUCT);
        return objError;
    }
    
    public static JSONObject getpostTagsJSONObj(PostObjTag tagobj){
        JSONObject object = new JSONObject();
        try {
            object.put("tags", tagobj==null?"":tagobj.getTags());
            object.put("deviceid",tagobj==null?"": tagobj.getDeviceid());
            object.put("productkey",tagobj==null?"": tagobj.getProductkey());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object;
    }
    
    
    public static JSONObject getEventJOSNobj(PostObjEvent event) {
        JSONObject localJSONObject = new JSONObject();
        try{
            localJSONObject.put("event_identifier", event.getEvent_id());
            localJSONObject.put("time", event.getTime());
            localJSONObject.put("version", event.getVersion());
            localJSONObject.put("appkey", event.getAppkey());
            localJSONObject.put("activity", event.getActivity());
            if (event.getLabel() != null)
                localJSONObject.put("label", event.getLabel());
            localJSONObject.put("acc", event.getAcc());
        } catch (JSONException localJSONException){
            CommonUtil.printLog("UmsAgent", "json error in emitCustomLogReport");
            localJSONException.printStackTrace();
        }
        return localJSONObject;
    }

    public static JSONObject getEventJOSNobjs(PostObjEvent event) {
        JSONObject localJSONObject = new JSONObject();
        try{
            localJSONObject.put("event_id", event.getEvent_id());
            localJSONObject.put("time", event.getTime());
            localJSONObject.put("version", event.getVersion());
            localJSONObject.put("appkey", event.getAppkey());
            localJSONObject.put("activity", event.getActivity());
            if (event.getLabel() != null)
                localJSONObject.put("label", event.getLabel());
            localJSONObject.put("acc", event.getAcc());
        } catch (JSONException localJSONException){
            CommonUtil.printLog("UmsAgent", "json error in emitCustomLogReport");
            localJSONException.printStackTrace();
        }
        return localJSONObject;
    }

    
    public static JSONObject getActivityJOSNobj(PostObjActivity activity) {
        JSONObject localJSONObject = new JSONObject();
        try{
            localJSONObject.put("session_id", activity.getSession_id());
            localJSONObject.put("start_millis", activity.getStart_millis());
            localJSONObject.put("end_millis", activity.getEnd_millis());
            localJSONObject.put("duration", activity.getDuration());
            localJSONObject.put("version", activity.getVersion());
            localJSONObject.put("activities", activity.getActivities());
            localJSONObject.put("appkey", activity.getAppkey());

        } catch (JSONException localJSONException)
        {
            CommonUtil.printLog("UmsAgent", "json error in emitCustomLogReport");
            localJSONException.printStackTrace();
        }
        return localJSONObject;
    }

    public static JSONObject getErrorJOSNobj(PostObjError error) {
        JSONObject localJSONObject = new JSONObject();
        try{
            localJSONObject.put("stacktrace", error.getStacktrace());
            localJSONObject.put("time", error.getTime());
            localJSONObject.put("version", error.getVersion());
            localJSONObject.put("activity", error.getActivity());
            localJSONObject.put("appkey", error.getAppkey());
            localJSONObject.put("os_version", error.getOs_version());
            localJSONObject.put("deviceid", error.getDeviceid());

        } catch (JSONException localJSONException)
        {
            CommonUtil.printLog("UmsAgent", "json error in emitCustomLogReport");
            localJSONException.printStackTrace();
        }
        return localJSONObject;
    }
    
}
